package cn.qayy.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
import cn.qayy.utils.ServletContextUtils;
import cn.qayy.utils.StringUtil;

/***
 * @author wy 
 * 好伙伴
 */

@SuppressWarnings("all")
public class GFDTO implements Serializable,Uploadable{
	private String name;
	private String fenzu;
	/**
	 * 修订标识
	 */
	private String isUpdate = "E";
	private String id;
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GFDTO other = (GFDTO) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFenzu() {
		return fenzu;
	}

	public void setFenzu(String fenzu) {
		this.fenzu = fenzu;
	}

	public String getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	@Override
	public Uploadable filter(Map<String, List<Uploadable>> map, Set<Uploadable> set, int num) throws Exception {
		/**容纳错误消息的list*/
		List<Uploadable> mlist = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		/***
		 * 过滤掉所有的空格
		 */
		String name = StringUtil.trimAll(this.getName());
	    String fenzu = StringUtil.trimAll(this.getFenzu());
	    String isUpdate =StringUtil.trimAll(this.getIsUpdate());
	    String id =StringUtil.trimAll(this.getId());
	    
	    /**
	     * 判断是否导入的模板是正确的模板
	     */
	    if(num == 1) {
	    	if(!CollectionConstant.goodfriden_mb.get(0).contains(name)
	    			||!CollectionConstant.goodfriden_mb.get(1).contains(fenzu)
	    			||!CollectionConstant.goodfriden_mb.get(2).contains(isUpdate)
	    			||!CollectionConstant.goodfriden_mb.get(3).contains(id)) {
	    		MsgInfo msg = new MsgInfo(Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_MB,Constant.ERROR_NUM+num);
				mlist.add(msg);
				map.put("stop", mlist);
		    	return null;
		    }
	    } else {
	    	/***
		     * 数据合格性校验
		     */
			if(!CollectionConstant.cuFlags.contains(isUpdate)) {
				MsgInfo msg = new MsgInfo(isUpdate,"修订标识",CollectionConstant.cuFlags.toString(),Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			/**
			 * 判断好伙伴姓名是否为空
			 */
			if(StringUtil.isBlank(name)) {
				MsgInfo msg = new MsgInfo(name,"好伙伴姓名",Constant.NOT_BLANK_MSG,Constant.ERROR_NUM+num);
				mlist.add(msg);
			}
			/***
			 * 返回处理后的结果数据
			 */
			if(!mlist.isEmpty()) {
				map.put("fail"+num, mlist);
			} 
			/***把验证通过的合格数据返回**/
			this.setAll(name,fenzu,isUpdate,id);
			set.add(this);
			return this;
	    }
	    return null;
	}

	@Override
	public String flag() throws Exception {
		return this.isUpdate;
	}

	public GFDTO() {
	}
	private void setAll(String name, String fenzu, String isUpdate, String id) {
		this.name=name;
		this.fenzu=fenzu;
		this.isUpdate=isUpdate;
		this.id=id;
	}
}
